/**
  * Copyright (c) CSIRO Australia, 2009
  *
  * @author $Author: jia020 $
  * @version $Id: IMSManager.java 643 2009-05-20 00:20:55Z jia020 $
  */
package au.csiro.nt.pdsp.ims;

import java.sql.ResultSet;
import java.sql.SQLException;

import au.csiro.nt.pdsp.util.Config;

/**
 * Class for managing messages
 * @author jia020
 *
 */
public class IMSManager {
	/**
	 * Construct XML message data records relating to the userid.
	 * @param userid
	 * @return XML message data.
	 */			
	public static String createXMLMyPendingMessages(String userid) {
		String sql = "select i.imsid,i.status,w.individualuserid userid,i.mobilefrom,i.mobileto,sentmessage,w.workflowid from ims i ,workflow w " +
				"where i.status = 'sent' and i.workflowid=w.workflowid and w.individualuserid = '"+userid+"'";
		ResultSet rs = Config.getInstance().mysql.execSelect(sql);
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		sb.append("<pdsp>");
		sb.append("<metadata>");	
		sb.append("<columnsdefinition>");
		sb.append("<column name=\"imsid\" type=\"integer\" />");
		sb.append("<column name=\"status\" type=\"string\" />");
		sb.append("<column name=\"userid\" type=\"string\" />");	
		sb.append("<column name=\"mobilefrom\" type=\"string\" />");		
		sb.append("<column name=\"mobileto\" type=\"string\" />");	
		sb.append("<column name=\"sentmessage\" type=\"string\" />");	
		sb.append("<column name=\"workflowid\" type=\"integer\" />");		
		sb.append("</columnsdefinition>");
		sb.append("</metadata>");
		try {
			sb.append("<records>");			
			while (rs!=null && rs.next()) {
				sb.append("<record>");
					sb.append("<imsid>");sb.append(rs.getInt("imsid"));sb.append("</imsid>");						
					sb.append("<status>");sb.append(rs.getString("status"));sb.append("</status>");			
					sb.append("<userid>");sb.append(rs.getString("userid"));sb.append("</userid>");					
					sb.append("<mobilefrom>");sb.append(rs.getString("mobilefrom"));sb.append("</mobilefrom>");			
					sb.append("<mobileto>");sb.append(rs.getString("mobileto"));sb.append("</mobileto>");		
					sb.append("<sentmessage>");sb.append(rs.getString("sentmessage"));sb.append("</sentmessage>");			
					sb.append("<workflowid>");sb.append(rs.getInt("workflowid"));sb.append("</workflowid>");									
				sb.append("</record>");			
			}
			sb.append("</records>");		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		sb.append("</pdsp>");		
		return sb.toString();		
	}
	/**
	 * Process the received message 
	 * @param imsid message id of the received message.
	 * @param message the content of the received message.
	 * @return boolean to indicate the success of processing.
	 */		
	public static boolean recvMessage(int imsid,String message) {
		String sql = "update ims set recvmessage='" + message + "' ,status='recv' where imsid="+imsid;
		boolean bOK = Config.getInstance().mysql2.execUpdate(sql);		
		return bOK;
	}
}
